<?php


namespace app\voip\controller;


use app\voip\func\CallFunc;
use app\voip\func\CallTaskFunc;
use app\voip\func\ExcelListFunc;
use app\voip\func\SmsFunc;
use app\voip\model\Sms;
use think\Db;
use vae\controller\AdminCheckAuth;

class CallTaskController extends AdminCheckAuth
{
    public function excelIndex()
    {

        return view();
    }
    public function excelAdd()
    {
        $where=[];
        $where['aid']=vae_get_login_admin('id');
        $userGroup=CallFunc::getGroupList($where);
        $smsType=SmsFunc::getTypeList();

        $where=[];
        $where['uid']=vae_get_login_admin('id');
        $resultExcel=ExcelListFunc::getExcelListFind($where,3);



        return view('',['userGroup'=>$userGroup,'smsType'=>$smsType,'excelList'=>$resultExcel]);
    }
    public function getGroupUserList()
    {
        $id=input('id');
        $aid=vae_get_login_admin('id');
        $where=[];
        $where['id']=$id;
        $where['aid']=$aid;

        $ginfo=CallFunc::getGroupInfo($where);

        $whereUser=[];
        $whereUser['number']=array('in',$ginfo['user']);
        $userInfo=CallFunc::gerGroupUserList($whereUser);
        return $userInfo;

    }

    public function getGroupSmsList()
    {
        $where=[];
        $where['tid']=input('id');
        $where['status']=1;
        $smsInfo=SmsFunc::getSmsInfoList($where);

        return $smsInfo;
    }
    public function ajaxGetList()
    {
        if (!request()->isAjax()){return view('index');}
        $where = array();
        $param = vae_get_param();

        if(!empty($param['keywords'])) {
            $where['id|name|aim|number'] = ['like', '%' . $param['keywords'] . '%'];
        }
        $rows = empty($param['limit']) ? \think\Config::get('paginate.list_rows') : $param['limit'];
        $where['aid']=vae_get_login_admin('id');
        return vae_assign_table(0,'',CallTaskFunc::getList($where,$rows,$param));
    }


    public function taskAdd()
    {
        try{
            Db::startTrans();
            $validate=validate('CallAuth');
            $data=input();
            $data['sms']=implode(',',$data['sms'] ?? []);
            $data['users']=implode(',',$data['users']);
            if (!$validate->scene(__FUNCTION__)->check($data)){
                throw new \Exception($validate->getError());
            }
            $data['aid']=vae_get_login_admin('id');
            $data['number']='H'.date("YmdHis").uniqid();
            $tid=CallTaskFunc::add($data);
            $users=explode(',',$data['users']);
            $userData=[];
            $users=array_unique($users, SORT_REGULAR);
            foreach ($users as $val)
            {
                $userData[]=['uid'=>$val,'tid'=>$tid];
            }

            CallTaskFunc::taskAddUserAll($userData);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();
    }


    /**
     * 任务列表
     */
    public function userTaskList()
    {
        return view();
    }

    public function ajaxGetTaskList()
    {
        if (!request()->isAjax()){return view('index');}
        $where = array();
        $param = vae_get_param();

        if(!empty($param['keywords'])) {
            $where['c1.id|c1.name|c1.aim'] = ['like', '%' . $param['keywords'] . '%'];
        }
        $rows = empty($param['limit']) ? \think\Config::get('paginate.list_rows') : $param['limit'];
        $where['c2.uid']=vae_get_login_admin('id');
        $where['c1.is_close']=1;

        return vae_assign_table(0,'',CallTaskFunc::getListAndUser($where,$rows,$param));
    }

    /**
     * @param $id
     */
    public function delTask($id)
    {
        try{
            CallTaskFunc::taskDel($id);
            CallTaskFunc::taskUserDel(['tid'=>$id]);
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();

    }

    /**
     * @param $id
     * @param $type
     * @return void
     */
    public function updateTypeTask($id,$type)
    {
        try{
            switch ($type){
                case '正在执行':
                    $type=2;
                    break;
                case '任务结束':
                    $type=1;
                    break;
            }
            CallTaskFunc::updateTask(['id'=>$id,'type'=>$type]);
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();

    }

    /**
     * @param $id
     * @param $close
     * @return void
     */
    public function updateCloseTask($id,$close)
    {
        try{
            switch ($close){
                case '正常显示':
                    $close=2;
                    break;
                case '显示关闭':
                    $close=1;
                    break;
            }
            CallTaskFunc::updateTask(['id'=>$id,'is_close'=>$close]);
        }catch (\Exception $e){
            return vae_assign(0,$e->getMessage());
        }
        return vae_assign();

    }

}